package dk.muldbjergskolen.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.JTextField;

import dk.muldbjergskolen.ctr.FagCtr;
import dk.muldbjergskolen.model.Fag;

import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JComboBox;

public class RetFag extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextPane txtpnKortform;
	private JButton okButton;
	private JButton cancelButton;
	private JTextField txtFag;
	private JTextField txtForkortelse;
	private JComboBox<Integer> comboBox_Fag;
	private FagCtr fCtr = new FagCtr();
	/**
	 * Create the dialog.
	 */
	public RetFag(RetFagListener listener) {
		
		retFagListener = listener;
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Ret fag");
		setBounds(100, 100, 230, 174);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JTextPane txtpnBlah = new JTextPane();
		txtpnBlah.setEditable(false);
		txtpnBlah.setBackground(UIManager.getColor("Button.background"));
		txtpnBlah.setText("Fag:");
		txtpnBlah.setBounds(10, 39, 57, 20);
		contentPanel.add(txtpnBlah);
		{
			txtpnKortform = new JTextPane();
			txtpnKortform.setText("Forkortelse:");
			txtpnKortform.setBackground(SystemColor.menu);
			txtpnKortform.setBounds(10, 70, 64, 20);
			contentPanel.add(txtpnKortform);
		}
		{
			txtFag = new JTextField();
			txtFag.setBounds(77, 39, 127, 20);
			contentPanel.add(txtFag);
			txtFag.setColumns(10);
		}
		
		txtForkortelse = new JTextField();
		txtForkortelse.setColumns(10);
		txtForkortelse.setBounds(77, 70, 127, 20);
		contentPanel.add(txtForkortelse);
		
		JLabel lblVlgFag = new JLabel("Vælg Fag:");
		lblVlgFag.setBounds(10, 11, 57, 14);
		contentPanel.add(lblVlgFag);
		
		comboBox_Fag = new JComboBox();
		comboBox_Fag.setModel(new DefaultComboBoxModel(new String[] {"Dansk"}));
		comboBox_Fag.setBounds(77, 8, 127, 20);
		contentPanel.add(comboBox_Fag);
		opdaterFag();
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						ret();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{txtFag, txtForkortelse, okButton, cancelButton}));
	}
	
	
	
	//Opdatere ComboBoxen klasser, så den indeholder de klasser der er i databasen.
	public void opdaterFag()
	{
		try
		{
			comboBox_Fag.removeAllItems();
			ArrayList<dk.muldbjergskolen.model.Fag> a = fCtr.findFagene();
		
			for(int i = 0; i < a.size(); i++)
			{
				comboBox_Fag.addItem(a.get(i).getId());
			}
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(this, "Ingen Fag er blev fundet.");
		}
	}
		
		//
		// Event håndtering.
		//

		// "Lyttere"
		RetFagListener retFagListener;


		public interface RetFagListener
		{
			void OnRettetFag();
		}

		/***
		 * Kaldes når en nyt fag er lavet.
		 */
		private void OnRettetFag()
		{
			if (retFagListener != null)
				retFagListener.OnRettetFag();
		}
		
		//Retter et fag
		public void ret()
		{
			try
			{
				int glId = Integer.parseInt(comboBox_Fag.getSelectedItem().toString());
				String glNavn = comboBox_Fag.getSelectedItem().toString();
				Fag f = new Fag(txtFag.getText(), txtForkortelse.getText());
			
				fCtr.retFag(f, glId);
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(txtFag, "Navn skal udfyldes");
				JOptionPane.showMessageDialog(txtForkortelse, "Forkortelse skal udfyldes");
				return;
			}
			OnRettetFag();
			setVisible(false);
		}
}
	

